package test;



class Goods {
    private String name;
    private boolean isDanger;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDanger() {
        return isDanger;
    }

    public void setDanger(boolean danger) {
        isDanger = danger;
    }
}

class DangerException extends Exception {
    public void toShow() {
        System.out.println("危险品!");
    }
}

class Machine {
    public void checkBag(Goods[] goods) throws DangerException {
        for (Goods good : goods) {
            if (good.isDanger()) {
                throw new DangerException();
            }
        }
        System.out.println("安全通过。");
    }
}

public class SecurityCheckSimulation {
    public static void main(String[] args) {
        Machine machine = new Machine();
        String[] names = {"苹果", "炸药", "西服", "硫酸", "手表", "硫磺"};
        Goods[] goods = new Goods[names.length];
        for (int i = 0; i < names.length; i++) {
            goods[i] = new Goods();
            goods[i].setName(names[i]);
            goods[i].setDanger(i % 2 != 0); // 偶数索引的物品不是危险品，奇数索引的是危险品
        }
        try {
            machine.checkBag(goods);
        } catch (DangerException e) {
            e.toShow();
        }
    }
}
